<?php
namespace Operations\Controller;
use Common\Controller\AdminbaseController;

class AdvertisingController extends AdminbaseController {
	protected $advertising_model;

	function _initialize() {
		parent::_initialize();
		$this->advertising_model= D("Advertising");
	}
    private function ad() {
		$adtype =
			array(
			array(
	            array('id' => '0', 'name'=>'请选择'),
	            array('id' => '1', 'name'=>'摇摇乐奖品页广告'),
	            array('id' => '2', 'name'=>'摇摇乐提现页广告')
			),
			array(
	            array('id' => '0', 'name'=>'请选择'),
	            array('id' => '1', 'name'=>'未启用'),
	            array('id' => '2', 'name'=>'已启用'),
	            array('id' => '3', 'name'=>'已停用'),
			),

		);
		return $adtype;
    }
	function index(){
		if(I('param.')){
            if(I('param.event_type')){
                    $search[] = array('event_type'=>I('param.event_type'));
                    $_GET['event_type'] = I('param.event_type');
            }
            if(I('param.start_time')){
                    $search[] = array('date(start_at)'=>array('egt',I('param.start_time')));
                    $_GET['start_time'] = I('param.start_time');
            }
            if(I('param.end_time')){
                    $search[] = array('date(end_at)'=>array('elt',I('param.end_time')));
                    $_GET['end_time'] = I('param.end_time');
            }
            if(I('param.keyword')){
                    $search[] = array('aname'=>array("like","%".I('param.keyword')."%"));
                    $_GET['keyword'] = I('param.keyword');
            }
		}
		$count=$this->advertising_model
		->where($search)
		->count();

		$page = $this->page($count, 50);

		$advertising=$this->advertising_model
		->where($search)
		->limit($page->firstRow . ',' . $page->listRows)
		->order("id DESC")
		->select();
		if($advertising){
			foreach($advertising as $auser){
				$uid[] =$auser['userid'];
			}
			$users=M("users")->field("id,user_login")->where(array('id'=>array('in',$uid)))->select();
			$this->assign("users", $users);
		}
		$ad = $this->ad();
		$this->assign("adtype",$ad[0]);
		$this->assign("ad", $advertising);
		$this->assign("Page", $page->show('Admin'));
		$this->assign("count", $count);
		$this->assign("urlimg", C('IMG_PATH'));
		$this->display();
	}

	function add(){
		$ad = $this->ad();
		$this->assign("adtype",$ad[0]);
		$this->display();
	}
	function add_post(){
		if (IS_POST) {
			if(!I('post.aname')){
				$this->error('请输入广告名称');
			}
			if(!I('post.atype')){
				$this->error('请选择广告类型');
			}
			$_POST['astatus'] = 1;
			$_POST['create_user'] = get_current_admin_id();
			$_POST['userid'] = get_current_admin_id();
			if($_FILES){
				$Aliyun = new \Aliyun\Controller\AliyunController();
				$ad_pic = $Aliyun->save($_FILES["update_img"],'img/'.CONTROLLER_NAME);
				if(!$ad_pic['error']){
					$_POST['adimg'] =$ad_pic['path'].$ad_pic['name'];
				}else{
					$this->error('图像上传失败');
				}
			}
			if($this->advertising_model->add($_POST)){
				$this->success("添加成功！",U('Advertising/index'));
			} else {
				$this->error("添加失败！");
			}
		}
	}

	public function edit(){
		$advertising = $this->advertising_model->where(array('id'=>I("get.id")))->find();
		$ad = $this->ad();
		$this->assign("adtype",$ad[0]);
		$this->assign("urlimg", C('IMG_PATH'));
		$this->assign("ad",$advertising);
		$this->display();
	}

	public function edit_post(){
		if (IS_POST) {
			if(!I('post.aname')){
				$this->error('请输入广告名称');
			}
			$_POST['userid'] = get_current_admin_id();
			if($_FILES){
				$Aliyun = new \Aliyun\Controller\AliyunController();
				$ad_pic = $Aliyun->save($_FILES["update_img"],'img/'.CONTROLLER_NAME);
				if(!$ad_pic['error']){
				$_POST['adimg'] =$ad_pic['path'].$ad_pic['name'];
				}else{
					$this->error('图像上传失败');
				}
			}
			if ($this->advertising_model->save($_POST)) {
				$this->success("修改广告成功！",U('Advertising/index'));
			} else {
				$this->error("修改广告失败！");
			}
		}
	}

	function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
				$ad = $this->advertising_model->where("id=$id")->find();
			if ($ad) {
				if($ad['adimg']){
					$Aliyun = new \Aliyun\Controller\AliyunController();
					$Aliyun->delete($ad['adimg']);
				}
				$this->advertising_model->where("id=$id")->delete();
				$this->success("广告删除成功");
			} else {
				$this->error($this->advertising_model->getError());
			}
		}
	}
	public function up(){
    	if (I("get.id")) {
    		$rst = $this->advertising_model->where(array('id'=>I("get.id")))->setField('astatus','2');
    		if ($rst) {
    			$this->success("广告启用成功！");
    		} else {
    			$this->error('广告启用失败！');
    		}
    	} else {
			$this->error($this->advertising_model->getError());
    	}
    }

    public function down(){
    	if (I("get.id")) {
    		$rst = $this->advertising_model->where(array('id'=>I("get.id")))->setField('astatus','3');
    		if ($rst) {
    			$this->success("广告停用成功！");
    		} else {
    			$this->error('广告停用失败！');
    		}
    	} else {
			$this->error($this->advertising_model->getError());
    	}
    }
}
